package org.example.script;

import org.example.constants.CommonConstants;
import org.example.model.CallBack;
import org.example.model.CallBackAndReturn;
import org.example.model.Device;
import org.example.model.Widget;
import org.example.script.utils.AdvertisementExecutor;
import org.example.script.utils.SwipeEnum;
import org.example.script.utils.SwipeExecutor;
import org.example.utils.CommonUtils;
import org.example.utils.GlobalUtils;
import org.openqa.selenium.WebDriver;

/**
 * 红果免费短剧执行器
 * @author root
 */
public class HgMfdjExecutor {

    private static final String saveFilePath = CommonConstants.saveImgPath;

    private WebDriver webDriver;

    private Device device;

    private AdvertisementExecutor advertisementExecutor;

    private SwipeExecutor swipeExecutor;

    public HgMfdjExecutor() {

    }

    public HgMfdjExecutor(WebDriver webDriver, Device device) {
        this.webDriver = webDriver;
        this.device = device;
    }

    public void execute() {
        if (!startApp()) {
            GlobalUtils.logi("启动失败，任务结束");
            return;
        }
        advertisementExecutor = new AdvertisementExecutor();
        String [] adEnterPageFlag = {".*后可领取奖励", "广告"};
        String [] adEndFlag = {".*已成功领取奖励.*"};
        String [] continueAdVideoFlag = {".*领取奖励.*", ".*继续观看.*", ".*再看一个.*"};
        String [] endAdVideoFlag = {".*坚持退出.*", ".*放弃奖励.*", ".*以后再说.*", ".*取消.*", ".*退出.*"};
        advertisementExecutor.initParams(webDriver, device, adEnterPageFlag, adEndFlag, continueAdVideoFlag, endAdVideoFlag, saveFilePath, new CallBackAndReturn() {
            @Override
            public boolean execute(Object... params) {
                return isTaskPage();
            }
        }, null);

        swipeExecutor = new SwipeExecutor();

        video();

        toTaskList();

        sign();

        openBox();

        advertisement();

        shopping();

        CommonUtils.clearBackApps(webDriver, device);
    }

    /**
     * 启动app
     * @return
     */
    private boolean startApp() {
        GlobalUtils.home(webDriver);
        GlobalUtils.sleep(2000);
        GlobalUtils.home(webDriver);
        String [] appArray = {".*红果免费短剧.*"};
        Widget widget = CommonUtils.findMoreWidgetByOcr(webDriver, appArray, saveFilePath, true);
        if (widget != null) {
            CommonUtils.myClick(webDriver, device, widget, 2000L);
            GlobalUtils.sleep(8000L);
            return true;
        }

        return false;
    }

    private void toTaskList() {
        String [] skipArray = {".*跳过.*"};
        CommonUtils.searchOcrWidgetAndClick(webDriver, device, skipArray, saveFilePath, true, 2000L, null);

        String [] taskArray = {".*福利.*"};
        CommonUtils.searchOcrWidgetAndClick(webDriver, device, taskArray, saveFilePath, true, 2000L, null);

        int total_try_times = 5;
        for (int i = 1; i <= total_try_times; i++) {
            if (!isTaskPage()) {
                GlobalUtils.logi("未进入任务页，尝试进入" + i + "次");
                CommonUtils.searchOcrWidgetAndClick(webDriver, device, taskArray, saveFilePath, true, 2000L, null);

                checkPopup();
            } else {
                break;
            }
        }
    }

    /**
     * 任务页判断
     * @return
     */
    private boolean isTaskPage() {
        String [] taskFlag = {".*签到礼包.*", ".*我的金币.*", ".*我的抵用金.*", ".*抵用金.*", "日常任务"};
        Widget widget = CommonUtils.findMoreWidgetByOcr(webDriver, taskFlag, saveFilePath);
        if (widget != null) {
            return true;
        }

        return false;
    }

    /**
     * 检测弹窗
     */
    private void checkPopup() {
        String [] popupArray = {"我知道了"};
        CommonUtils.searchOcrWidgetAndClick(webDriver, device, popupArray, saveFilePath, true, 2000L, null);
    }

    /**
     * 签到
     */
    private void sign() {
        String [] signAlertArray = {".*立即签到.*", ".*立即领取.*"};
        CommonUtils.searchOcrWidgetAndClick(webDriver, device, signAlertArray, saveFilePath, true, 2000L, null);

        String [] videoAlertArray = {".*看视频再领.*", ".*看视频.*"};
        CommonUtils.searchOcrWidgetAndClick(webDriver, device, videoAlertArray, saveFilePath, true, 2000L, new CallBack() {
            @Override
            public void execute(Object... params) {
                toTaskList();
            }
        });
        String [] adAlertArray = {"看视频最高得.*", "看视频至少可得.*", "看内容最高可得.*", "看广告再得.*", "看广告最高.*"};
        CommonUtils.searchOcrWidgetAndClick(webDriver, device, adAlertArray, saveFilePath, true, 2000L, new CallBack() {
            @Override
            public void execute(Object... params) {
                advertisementExecutor.execute();
            }
        });
    }

    /**
     * 开宝箱
     */
    private void openBox() {
        String [] openBoxArray = {".*开宝箱得金币.*", ".*点就领.*"};
        CommonUtils.searchOcrWidgetAndClick(webDriver, device, openBoxArray, saveFilePath, true, 2000L, null);
        String [] adAlertArray = {".*看内容最高可得.*"};
        CommonUtils.searchOcrWidgetAndClick(webDriver, device, adAlertArray, saveFilePath, true, 2000L, new CallBack() {
            @Override
            public void execute(Object... params) {
                advertisementExecutor.execute();
            }
        });
    }

    /**
     * 广告任务
     */
    private void advertisement() {
        String [] adArray = {".*看视频得.*", ".*看广告得.*", ".*看广告赚.*", ".*看.*个视频领金币.*"};
        CommonUtils.searchOcrWidgetWithSwipeAndClick(webDriver, device, adArray, saveFilePath, true, 2000L, new CallBack() {
            @Override
            public void execute(Object... params) {
                advertisementExecutor.execute();
            }
        });
    }

    /**
     * 逛街
     */
    private void shopping() {
        String [] array = {".*逛街领金币.*", ".*去逛街.*", ".*去领钱.*"};
        swipeExecutor.initParams(webDriver, device, "逛街", new CallBackAndReturn() {
            @Override
            public boolean execute(Object... params) {
                // 进入页面
                CommonUtils.searchOcrWidgetWithSwipeAndClick(webDriver, device, array, saveFilePath, true, 2000L, null);
                return true;
            }
        }, new CallBack() {
            @Override
            public void execute(Object... params) {
                // 检测弹窗
            }
        }, 5000, 8000, 10, SwipeEnum.SWIPE_UP, null, new CallBack() {
            @Override
            public void execute(Object... params) {
                // 返回任务页
                GlobalUtils.back(webDriver);
                String [] backArray = {".*放弃奖励.*"};
                for (int i = 0; i < 3; i++) {
                    CommonUtils.searchOcrWidgetAndClick(webDriver, device, backArray, saveFilePath, true, 2000L, null);
                }
            }
        }, null);
        swipeExecutor.execute();
    }

    private void video() {
        swipeExecutor.initParams(webDriver, device, "视频", new CallBackAndReturn() {
            @Override
            public boolean execute(Object... params) {
                // 进入页面
                return true;
            }
        }, new CallBack() {
            @Override
            public void execute(Object... params) {
                // 检测弹窗
                checkPopup();
            }
        }, 5000, 8000, 10, SwipeEnum.SWIPE_UP, null, new CallBack() {
            @Override
            public void execute(Object... params) {
                // 退出页面

            }
        }, null);
        swipeExecutor.execute();
    }
}
